package com.sky.controller.User;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单
 */
@RestController
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    OrderService orderService;


    /*
     * 下单
     *
     * */
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        //1.接受下单的请求参数: json,实体类+@RequestBody
        log.info("用户下单：{}", ordersSubmitDTO);
        //2.调用service实现下单
        OrderSubmitVO orderSubmitVO = orderService.submit(ordersSubmitDTO);
        //3.封装处理结果
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /*
     * 查询历史订单
     *
     * */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> page(int page, int pageSize, Integer status) {
        PageResult pageResult = orderService.pageQuery4User(page, pageSize, status);
        return Result.success(pageResult);
    }

    /*
     *查询订单详情
     *
     * */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> getById(@PathVariable Long id) {
        OrderVO orderVO = orderService.getById(id);
        return Result.success(orderVO);
    }

    /*
     *取消订单
     *
     * */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result Canceltheorder(@PathVariable Long id) {

        orderService.Canceltheorder(id);
        return Result.success();
    }

    /*
     *再来一单
     *
     * */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result Onemoreorder(@PathVariable Long id) {

        orderService.Onemoreorder(id);
        return Result.success();
    }

    /*
     * 客户催单
     *
     * */
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){
        //1.接受请求参数：path：Long id
        log.info("客户催单：{}",id);
        //2.调用service实现催单
        orderService.reminder(id);
        //3.封装/返回处理结果
        return Result.success();//code =1
    }
}
